#ifndef LEVELMANAGER_H
#define LEVELMANAGER_H

#include <QObject>
#include <QVector>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QDir>
#include <QRandomGenerator>

struct SudokuLevel {
    int levelId;
    QString name;
    int difficulty; // 1-5
    QVector<QVector<int>> puzzle;
    QVector<QVector<int>> solution;
    int timeLimit; // in seconds
    
    SudokuLevel() : levelId(0), difficulty(1), timeLimit(0) {}
};

class LevelManager : public QObject
{
    Q_OBJECT

public:
    explicit LevelManager(QObject *parent = nullptr);
    
    bool loadLevels();
    bool saveLevels();
    SudokuLevel getLevel(int levelId) const;
    int getLevelCount() const { return m_levels.size(); }
    QVector<SudokuLevel> getAllLevels() const { return m_levels; }
    
    void generateRandomLevel(int difficulty, int levelId = -1);
    void addLevel(const SudokuLevel &level);
    bool removeLevel(int levelId);

private:
    void generateSudokuPuzzle(int difficulty, 
                             QVector<QVector<int>> &puzzle, 
                             QVector<QVector<int>> &solution);
    bool solveSudoku(QVector<QVector<int>> &grid);
    bool findEmptyCell(const QVector<QVector<int>> &grid, int &row, int &col);
    bool isValid(const QVector<QVector<int>> &grid, int row, int col, int num);
    void initializeDefaultLevels();
    
    QVector<SudokuLevel> m_levels;
    QString m_levelsFilePath;
};

#endif